/*
 * Created on Wed Jun 06 2018 by Ronnie Ren (zhaosong.ren) from Accenture
 *
 * Copyright (c) 2018 DingXing information & technology limited company.
 */

import { Injectable } from "@angular/core";
// import { UserService } from "../models/transform.service";
import { SqliteService } from "../../services/sqliteDB/sqlite.service";
// import { BillService } from "./bill.service";
import { DistrictDto } from "../dto/district.dto";
import { District } from "../transform/district";
import { JsonMappingUtil } from "../json-mapping-util";
import { Observable } from "rxjs/Observable";
import { SyncUtilService } from "../../services/offline/sync.util.service";
import { CurdService } from "./curd.service";




@Injectable()
export class DistrictService {

  constructor(
    private sqliteService: SqliteService, 
    private syncUtilService: SyncUtilService, 
    private curdService: CurdService) {

  }


  fetchByDistrictId(districtId: string): Observable<District> {

    return this.curdService.observableFetchObjects(DistrictDto , {id: districtId})
    .map((objs: Array<{}>) => {
        if (objs && objs.length > 0) {
            let dto = new DistrictDto();
            return JsonMappingUtil.MappingAllProp(objs[0], dto);
        }
        return null;
    })
    .map((dto) => {
        if (dto) {
            let model = new District({});
            return JsonMappingUtil.MappingAllProp(dto, model);
        }
        return null;
        
    });
    // let districtDto = new DistrictDto();
    // return Observable.fromPromise(
    //   this.sqliteService.executeSqlWithResults<DistrictDto>(`select * from ${districtDto.table().tableName} ` +
    //     ` where id = '${districtId}'`, DistrictDto)
    // ).map((objs: Array<{}>) => {
    //   console.log('123456789' + objs);
    //   if (objs && objs.length > 0) {
    //     let dto = new DistrictDto();
    //     return JsonMappingUtil.MappingAllProp(objs[0], dto); 
    //   }
    //   return null;
    // } ).map((dto) => {
    //   if (dto) {
    //     let model = new District({});
    //     return JsonMappingUtil.MappingAllProp(dto, model);
    //   }
    //   return null;
    // });
  }


  fetchAllDistrictIds(): Observable<Array<string>> {
    return this.syncUtilService.fetchPropertyWithArray(DistrictDto, "id");
  }



}
